<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - scene - multiple - compare</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			.container {
				position: absolute;
				width: 100%;
				height: 100%;
			}

			.slider {
				position: absolute;
				cursor: ew-resize;

				width: 40px;
				height: 40px;
				background-color: #F32196;
				opacity: 0.7;
				border-radius: 50%;

				top: calc(50% - 20px);
				left: calc(50% - 20px);
			}
		</style>
	</head>

	<body>

		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple scenes comparison<br />
		</div>

		<div class="container">
			<div class="slider"></div>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

			let container, camera, renderer, controls;
			let sceneL, sceneR;

			let sliderPos = window.innerWidth / 2;

			init();

			function init() {

				container = document.querySelector( '.container' );

				sceneL = new THREE.Scene();
				sceneL.background = new THREE.Color( 0xBCD48F );

				sceneR = new THREE.Scene();
				sceneR.background = new THREE.Color( 0x8FBCD4 );

				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
				camera.position.z = 6;

				controls = new OrbitControls( camera, container );

				const light = new THREE.HemisphereLight( 0xffffff, 0x444444, 3 );
				light.position.set( - 2, 2, 2 );
				sceneL.add( light.clone() );
				sceneR.add( light.clone() );

				initMeshes();
				initSlider();

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setScissorTest( true );
				renderer.setAnimationLoop( animate );
				container.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

			}

			function initMeshes() {

				const geometry = new THREE.IcosahedronGeometry( 1, 3 );

				const meshL = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
				sceneL.add( meshL );

				const meshR = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { wireframe: true } ) );
				sceneR.add( meshR );

			}

			function initSlider() {

				const slider = document.querySelector( '.slider' );

				function onPointerDown() {

					if ( event.isPrimary === false ) return;

					controls.enabled = false;

					window.addEventListener( 'pointermove', onPointerMove );
					window.addEventListener( 'pointerup', onPointerUp );

				}

				function onPointerUp() {

					controls.enabled = true;

					window.removeEventListener( 'pointermove', onPointerMove );
					window.removeEventListener( 'pointerup', onPointerUp );

				}

				function onPointerMove( e ) {

					if ( event.isPrimary === false ) return;

					sliderPos = Math.max( 0, Math.min( window.innerWidth, e.pageX ) );

					slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + 'px';

				}

				slider.style.touchAction = 'none'; // disable touch scroll
				slider.addEventListener( 'pointerdown', onPointerDown );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
				renderer.render( sceneL, camera );

				renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
				renderer.render( sceneR, camera );

			}

		</script>
	</body>
</html>
